package com.waymap.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.waymap.service.LocationBinsService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.waymap.service.TransactionInventoryDetailsService;
import com.waymap.service.UserService;
import com.waymap.utils.CommonEnum;
import com.waymap.utils.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * Created by tim on 2018/7/19.
 * 用于数据同步（NS---->WMS）
 */
@Api(value = "WMS系统", tags = { "WMS-数据同步" })
@CrossOrigin
@RestController
@RequestMapping("sync")
@Slf4j
public class SyncController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TransactionInventoryDetailsService transactionInventoryDetailsService;
    @Autowired
    private UserService userService;
    @Autowired
    private LocationBinsService locationBinsService;

    /**
     * 接收NS数据并插入到WMS表里
     */
    @ApiOperation(value = "同步transactions和transaction_inventory_details数据到wms")
    @RequestMapping(value = "/syncTrancationDataFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncTrancationData(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncTrancationData(json);

    }

    /**
     * 同步locations数据到wms
     */
    @ApiOperation(value = "同步locations数据到wms")
    @RequestMapping(value = "/syncLocationsFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncLocationsFromNS(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncLocationsFromNS(json);

    }

    /**
     * 修改sowave拣选框
     */
    @ApiOperation(value = "修改sowave拣选框")
    @RequestMapping(value = "/syncSOWaveFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncSOWaveFromNS(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncSOWaveFromNS(json);
    }

    /**
     * 修改sowave拣选框
     */
    @ApiOperation(value = "删除销售单")
    @RequestMapping(value = "/deleteSOFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> deleteSOFromNS(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.deleteSOFromNS(json);
    }

    /**
     * 同步locations数据到wms
     */
    @ApiOperation(value = "同步items数据到wms")
    @RequestMapping(value = "/syncItemsFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncItemsFromNS(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncItemsFromNS(json);

    }

    @ApiOperation(value = "同步netSuite收货计划数据到WMS")
    @RequestMapping(value = "/getItemReceiptPlanData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getItemReceiptPlanData(@RequestBody String json) {
        if ((json ==null)||("".equals(json))) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        logger.info("接收的数据:"+json);
        return null;
      //  return transactionInventoryDetailsService.getItemReceiptPlanData(json);

    }
    
    @ApiOperation(value = "清空库存数据到WMS")
    @RequestMapping(value = "/clearItemInventory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> clearItemInventory(@RequestBody String accountId) {
        if ((accountId ==null)||("".equals(accountId))) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        logger.info("接收的数据:"+accountId);
        logger.info("删除库存");
        return transactionInventoryDetailsService.clearItemInventory(accountId);
    }


    @ApiOperation(value = "同步库存数据到WMS")
    @RequestMapping(value = "/syncItemInventoryFromNS", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncItemInventoryFromNS(@RequestBody Map itemInv) {
        if ((itemInv ==null)||(itemInv.isEmpty())) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        logger.info("接收的数据:"+itemInv);
        return transactionInventoryDetailsService.syncItemInventoryFromNS(itemInv);
    }
    
    @ApiOperation(value = "同步用户到WMS")
    @RequestMapping(value = "/syncUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncUser(@RequestBody Map userInfo) {
        if ((userInfo ==null)||(userInfo.isEmpty())) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        logger.info("接收的数据:"+userInfo);
        return userService.syncUser(userInfo);
    }

    @ApiOperation(value = "同步仓位到WMS")
    @RequestMapping(value = "/syncLocationBins", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> syncLocationBins(@RequestBody Map syncLocationBins) {
        logger.info("同步仓位到WMS");
        if ((syncLocationBins ==null)||(syncLocationBins.isEmpty())) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        logger.info("接收的数据:"+syncLocationBins);
        return locationBinsService.addLocationBins(syncLocationBins);
    }


    @ApiOperation(value = "同步库存数据到WMSV_V2")
    @RequestMapping(value = "/syncItemInventoryFromNS_v2", method = RequestMethod.POST)
    public ResponseEntity<Map> syncItemInventoryFromNS_v2(@RequestBody String json) {
        if (StringUtil.isNullOrEmpty(json)) {
            Map<String, Object> map = new HashMap(16);
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncItemInventoryFromNS(json);
    }


    @ApiOperation(value = "同步单据到WMS")
    @RequestMapping(value = "/syncDataFromNS", method = RequestMethod.POST)
    public ResponseEntity<Map> syncInventoryFromNS(@RequestBody String json) {
        Map<String, Object> map = new HashMap(16);
        if (StringUtil.isNullOrEmpty(json)) {
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.syncDataFromNS(json);
    }

    @ApiOperation(value = "重置WAVE状态")
    @RequestMapping(value = "/updateNsWave", method = RequestMethod.POST)
    public ResponseEntity<Map> updateNsWave(@RequestBody String json) {
        Map<String, Object> map = new HashMap(16);
        if (StringUtil.isNullOrEmpty(json)) {
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.updateNsWave(json);
    }

    @ApiOperation(value = "更新盘点单的状态")
    @RequestMapping(value = "/updateICstatus", method = RequestMethod.POST)
    public ResponseEntity<Map> updateICstatus(@RequestBody String json) {
        Map<String, Object> map = new HashMap(16);
        if (StringUtil.isNullOrEmpty(json)) {
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.updateICstatus(json);
    }

    @ApiOperation(value = "更新wms数据的报错")
    @RequestMapping(value = "/updateWmsError", method = RequestMethod.POST)
    public ResponseEntity<Map> updateWmsError(@RequestBody String json) {
        Map<String, Object> map = new HashMap(16);
        if (StringUtil.isNullOrEmpty(json)) {
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", "传入参数为空");
            return ResponseEntity.ok(map);
        }
        return transactionInventoryDetailsService.updateWmsError(json);
    }
}
